﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Teacher_ListStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int couresID = Convert.ToInt32(Request.QueryString["id"].ToString());

            UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("TEA"))
                {
                    if (udm.createConnection() != null && udm.showCoursesByID(udm.createConnection(), couresID) != null)
                    {
                        int id = Convert.ToInt32(Session["EmployeeID"].ToString());

                        if (Convert.ToInt32(udm.showCoursesByID(udm.createConnection(), couresID).Tables[0].Rows[0][4].ToString()) == id)
                        {
                            if (udm.showCoursesByID(udm.createConnection(), couresID).Tables[0].Rows.Count != 0)
                            {
                                gvCourseInformation.DataSource = udm.showCoursesByID(udm.createConnection(), couresID);
                                if (!Page.IsPostBack)
                                {
                                    gvCourseInformation.DataBind();
                                }

                                gvListStudent.DataSource = udm.showStudentsOfCourse(udm.createConnection(), couresID);
                                if (!Page.IsPostBack)
                                {
                                    gvListStudent.DataBind();
                                }
                            }
                            else
                            {
                                Label1.Text = "Course closed!";
                                Label2.Text = "";
                            }
                        }
                        else
                        {
                            Response.Redirect("~/LoginMessage.aspx?id=1");
                        }
                    }
                    else
                    {
                        Response.Redirect("~/ErrorPage.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            } 
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCourseInformation.EditIndex = e.NewEditIndex;
        gvCourseInformation.DataBind();
    }
    protected void dvCourseInformation_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCourseInformation.EditIndex = -1;
        gvCourseInformation.DataBind();
    }
    protected void dvCourseInformation_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = gvCourseInformation.EditIndex;
        CheckBox cb = (CheckBox)gvCourseInformation.Rows[index].Cells[15].Controls[0];

        if (cb.Checked)
        {
            UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

            int couresID = Convert.ToInt32(Request.QueryString["id"].ToString());

            udm.updateCourseEnd(udm.createConnection(), couresID);

            gvCourseInformation.EditIndex = -1;
            gvCourseInformation.DataBind();

            Response.Redirect("~/Teacher/ScoresManagerment.aspx");
        }
    }
    protected void gvListStudent_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvListStudent.EditIndex = e.NewEditIndex;
        gvListStudent.DataBind();
    }
    protected void gvListStudent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvListStudent.EditIndex = -1;
        gvListStudent.DataBind();
        lblError.Text = "";
    }
    protected void gvListStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = gvListStudent.EditIndex;
        TextBox tb = (TextBox)gvListStudent.Rows[index].Cells[3].Controls[1];

        string mark = tb.Text;

        CheckValid cv = new CheckValid();

        if (cv.checkMark(mark) == 1)
        {
            lblError.Text = "Please! Input Mark!";
        }
        else if (cv.checkMark(mark) == 2)
        {
            lblError.Text = "Mark only allow digit!";
        }
        else if (cv.checkMark(mark) == 3)
        {
            lblError.Text = "Mark must on range 0 to 10!";
        }
        else
        {
            decimal iMark = Convert.ToDecimal(mark);

            int couresID = Convert.ToInt32(Request.QueryString["id"].ToString());

            string stuID = gvListStudent.Rows[index].Cells[1].Text;

            UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

            if (!udm.updateMarkByStudentID(udm.createConnection(), stuID, couresID, iMark))
            {
                Response.Redirect("~/ErrorPage.aspx");
            }

            lblError.Text = "";

            gvListStudent.EditIndex = -1;
            gvListStudent.DataSource = udm.showStudentsOfCourse(udm.createConnection(), couresID);
            gvListStudent.DataBind();
        }
    }
}